<?php
class Delivery_Model_DbTable_OpcionesFamilias
	extends Delivery_Model_DbTable_Abstract

{
	protected $_name = 'opciones_familias';
	protected $_primary = 'idOpcionFamilia';
	
	public function insert( $familia ){
		
		return parent::insert(		
			$this->_getData( $familia )
		);	
	}
	
	public function update( $familia ){
		return parent::update(
			$this->_getData( $familia )  ,
			array('idOpcionFamilia = ?' => $familia->idOpcionFamilia)
		
		);
	}
	
	public function delete( $familia ){
		return parent::delete(
			array( 'idOpcionFamilia = ?' => $familia->idOpcionFamilia)
		);
	}
	
	public function getById( $idOpcionFamilia ){
		
		return 
			$this->_db->fetchRow(
				$this->select()->where('idOpcionFamilia = ?', $idOpcionFamilia, Zend_Db::INT_TYPE)
			);
	}
	
	/**
	 * @param array $ids
	 * @return array:
	 */
	public function getByIds( $ids )
	{
		return $this->getAdapter()->fetchAll(
			$this->select()->where( 'idOpcionFamilia IN (?)', $ids, Zend_Db::INT_TYPE),
			null,
			Zend_Db::FETCH_ASSOC
		);		
	}
	
	
	public function getAll(){
					
		return 
			$this->_db->fetchAssoc(
				$this->select()					
			);
	}
	
	public function getByParams( $params, $offset = null, $limit = null, $order = null ){
		
		$select = $this->select();

	    if ( isset($params['idNegocio'])){
			$select->where('idNegocio = ?', $params['idNegocio'], Zend_Db::INT_TYPE);
		}
		
		if ( $order ){
			$select->order( $order );
		}
		
		if ( $limit && !is_null($offset) ){
			$select->limit( $limit, $offset );
		}
		
		return $this->_db->fetchAssoc($select);
	}
	
	public function countByParams( $params )
	{
		$select = $this->select()
				->from($this, array('count' => 'COUNT(1)'));
		
	    if ( isset($params['idNegocio'])){
			$select->where('idNegocio = ?', $params['idNegocio'], Zend_Db::INT_TYPE);
		}		
				
		$result = $this->_db->fetchRow($select);
		
		return $result['count'];
	}
	
    protected function _getData( $opcionFamilia )
	{
		return array (			
   			'idNegocio'	    => $opcionFamilia->idNegocio,
   			'nombre'	    => $opcionFamilia->nombre,
   			'nombreInterno'	=> $opcionFamilia->nombreInterno
   		);	
	}
	
}